<?php

/**
 * This is the model class for table "items".
 *
 * The followings are the available columns in table 'items':
 * @property integer $identification
 * @property integer $library
 * @property string $name
 * @property string $url
 * @property string $types
 * @property string $tooltips
 * @property string $tags
 * @property string $note
 * @property string $insert
 * @property string $update
 * @property string $delete
 *
 * The followings are the available model relations:
 * @property Library $library0
 */
class Items extends CActiveRecord
{
    public $key;

   /**
    * Returns the static model of the specified AR class.
    * @param string $className active record class name.
    * @return Items the static model class
    */
   public static function model($className=__CLASS__)
   {
      return parent::model($className);
   }

   /**
    * @return string the associated database table name
    */
   public function tableName()
   {
      return 'items';
   }

   /**
    * @return array validation rules for model attributes.
    */
   public function rules()
   {
      // NOTE: you should only define rules for those attributes that
      // will receive user inputs.
      return array(
         array('library, name, url, insert', 'required'),
         array('library', 'numerical', 'integerOnly'=>true),
         array('name, url, tooltips, tags', 'length', 'max'=>255),
         array('types', 'length', 'max'=>10),
         array('note, update, delete', 'safe'),
         // The following rule is used by search().
         // Please remove those attributes that should not be searched.
         array('identification, library, name, url, types, tooltips, tags, note, insert, update, delete', 'safe', 'on'=>'search'),
      );
   }

   /**
    * @return array relational rules.
    */
   public function relations()
   {
      // NOTE: you may need to adjust the relation name and the related
      // class name for the relations automatically generated below.
      return array(
         'library0' => array(self::BELONGS_TO, 'Library', 'library'),
      );
   }

   /**
    * @return array customized attribute labels (name=>label)
    */
   public function attributeLabels()
   {
      return array(
         'identification' => 'Identification',
         'library' => 'Library',
         'name' => 'Name',
         'url' => 'Url',
         'types' => 'Types',
         'tooltips' => 'Tooltips',
         'tags' => 'Tags',
         'note' => 'Note',
         'insert' => 'Insert',
         'update' => 'Update',
         'delete' => 'Delete',
      );
   }

   /**
    * Retrieves a list of models based on the current search/filter conditions.
    * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
    */
   public function search()
   {
      // Warning: Please modify the following code to remove attributes that
      // should not be searched.

      $criteria=new CDbCriteria;

      $criteria->compare('identification',$this->identification);
      $criteria->compare('library',$this->library);
      $criteria->compare('name',$this->name,true);
      $criteria->compare('url',$this->url,true);
      $criteria->compare('types',$this->types,true);
      $criteria->compare('tooltips',$this->tooltips,true);
      $criteria->compare('tags',$this->tags,true);
      $criteria->compare('note',$this->note,true);
      $criteria->compare('insert',$this->insert,true);
      $criteria->compare('update',$this->update,true);
      $criteria->compare('delete',$this->delete,true);

      return new CActiveDataProvider($this, array(
         'criteria'=>$criteria,
      ));
   }


   /** getDataByParameter =============================
    * @author   TuanNguyen
    * @desc     save image entity to database
    * @param    $i_page: page to get next data
    *           $i_amountPerPage: amount data per page to display
    *           $s_order: string is defined for order of field
    *           $s_where: condition to get data
    *           $s_where: reference parameter to set all amount of data
    * @return   boolean
    *           ErrorEntity: if there are an error in database
    * ============================================== */
   public static function getDataByParameter($i_page, $i_amountPerPage, $s_order, $s_where, &$i_total)
   {
       //prepare order statement
       $i_total = Yii::app()->db->createCommand("Select Count(*) from `items` where ".$s_where.";")->queryScalar();;
   
       $command = Yii::app()->db->createCommand("CALL items_01_getList(:p_page,:p_amount,:p_oder,:p_where)");
       $command->bindValue("p_page",$i_page);
       $command->bindValue("p_amount",$i_amountPerPage);
       $command->bindValue("p_oder",$s_order);
       $command->bindValue("p_where",$s_where);
   
       try
       {
           $arr_model = array();
           $arr_data = $command->queryAll();
   
           foreach ( $arr_data as $key => $row)
           {
               $m_model = new Items();
               $m_model->unsetAttributes();
   
               $m_model->key = $row["identification"];
               $m_model->name = $row["name"];
               $m_model->url = $row["url"];
               $m_model->types = $row["types"];
               $m_model->tooltips = $row["tooltips"];
               $m_model->tags = $row["tags"];
               $m_model->note = $row["note"];
               $m_model->insert = $row["insert"];
               $m_model->update = $row["update"];
               $m_model->delete = $row["delete"];
   
               array_push($arr_model, $m_model);
           }//End foreach
   
           return $arr_model;
       }//End Try
       catch (CDbException $ex)
       {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_1";
            $o_error->param     = null;
            return $o_error;
       }//End catch
   }//End getDataByParameter
   
   /** addImagesToDb =============================
    * @author   TuanNguyen
    * @desc     save image entity to database
    * @param    string $p_insertStaement: string for values in insert statement
    * @return   boolean
    *           ErrorEntity: if there are an error in database
    * ============================================== */
   public static function addItemsToDb($s_insertStatement)
   {
       $command = Yii::app()->db->createCommand("CALL items_03_insert(:p_insertStaement)");
       $command->bindValue("p_insertStaement",$s_insertStatement);
   
       try
       {
           $i_result = $command->execute();
   
           if( $i_result == 0 )
               return true;
           else
               return false;
       }//End Try
       catch (CDbException $ex)
       {
           $o_error            = new ErrorEntity();
           $o_error->errCode   = "ERR_1";
           $o_error->param     = null;
           return $o_error;
       }//End catch
   }//End addImagesToDb
}